package com.woniuxy.carlife.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.carlife.domain.ServiceRoyalty;
import com.woniuxy.carlife.mapper.ServiceRoyaltyMapper;
import com.woniuxy.carlife.minio.MinioTemplate;
import com.woniuxy.carlife.param.ServiceRoyaltyParam;
import com.woniuxy.carlife.service.ServiceRoyaltyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @author fx
 * @ClassName:ServiceRoyaltyServiceImpl
 * @Description: 抽成规则service实现类
 * @date 2021/4/6 20:00
 **/

@Service
public class ServiceRoyaltyServiceImpl extends ServiceImpl<ServiceRoyaltyMapper, ServiceRoyalty> implements ServiceRoyaltyService {

    @Resource
    private MinioTemplate mt;

    @Resource
    private ServiceRoyaltyMapper mapper;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 功能描述〈根据服务类别id新增抽成规则〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 15:26
     * @throws
     */
    @Override
    public Boolean saveServiceRoyalty(ServiceRoyaltyParam royaltyParam) {
        ServiceRoyalty royalty = new ServiceRoyalty();
        //抽成规则名
        royalty.setRoyaltyRuleName(royaltyParam.getRoyaltyRuleName());
        //规则详情介绍
        royalty.setRoyaltyRuleInfo(royaltyParam.getRoyaltyRuleInfo());
        //服务类别id
        royalty.setServiceCategoryId(royaltyParam.getServiceCategoryId());
        //平台抽成比率
        royalty.setRoyaltyRatio(royaltyParam.getRoyaltyRatio());
        //状态，默认为使用
        royalty.setRoyaltyRuleStatus("使用");
        //创建时间
        royalty.setRuleCreateTime(new Timestamp(new Date().getTime()));
        //存储
        int insert = mapper.insert(royalty);
        if (insert>0){
            return true;
        }
        return false;
    }

    /**
     * 功能描述〈查询所有抽成规则信息〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 15:35
     * @throws
     */
    @Override
    public List<ServiceRoyalty> findAllServiceRoyalty() {
        List<ServiceRoyalty> serviceRoyalties = mapper.selectList(null);
        return serviceRoyalties;
    }

    /**
     * 功能描述〈根据类别id查询信息〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 15:44
     * @throws
     */
    @Override
    public ServiceRoyalty findServiceRoyaltyByCatagoryId(Integer categoryId) {
        ServiceRoyalty royalty = mapper.selectOne(new QueryWrapper<ServiceRoyalty>()
                .eq("service_category_id", categoryId));
        return royalty;
    }

    /**
     * 功能描述〈根据类别id修改抽成信息和状态信息〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 15:45
     * @throws
     */
    @Override
    public Boolean updateServiceRoyaltyByCatagoryId(ServiceRoyaltyParam royaltyParam) {
        ServiceRoyalty royalty = new ServiceRoyalty();
        //抽成规则名
        royalty.setRoyaltyRuleName(royaltyParam.getRoyaltyRuleName());
        //规则详情介绍
        royalty.setRoyaltyRuleInfo(royaltyParam.getRoyaltyRuleInfo());
        //服务类别id
        royalty.setServiceCategoryId(royaltyParam.getServiceCategoryId());
        //平台抽成比率
        royalty.setRoyaltyRatio(royaltyParam.getRoyaltyRatio());
        //规则状态
        royalty.setRoyaltyRuleStatus(royaltyParam.getRoyaltyRuleStatus());
        //根据类别id修改
        int service_category_id = mapper.update(royalty,new QueryWrapper<ServiceRoyalty>()
                                    .eq("service_category_id",
                                            royalty.getServiceCategoryId()));
        if (service_category_id>0){
            return true;
        }
        return false;
    }
}
